<?php

use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;

return new class extends Migration
{
    /**
     * Run the migrations.
     */
    public function up(): void
    {
        Schema::create('probation_records', function (Blueprint $table) {
            $table->id();
            $table->timestamps();

            // 员工信息
            $table->unsignedBigInteger('employee_id');
            $table->string('employee_name', 100)->nullable();
            $table->string('employee_number', 50)->nullable();

            // 试岗基本信息
            $table->string('probation_number', 50)->unique(); // 试岗编号
            $table->string('probation_type', 50); // leadership_promotion, position_change, new_hire
            $table->string('probation_reason', 200); // 试岗原因

            // 原岗位信息
            $table->unsignedBigInteger('original_department_id')->nullable();
            $table->string('original_department_name', 100)->nullable();
            $table->unsignedBigInteger('original_position_id')->nullable();
            $table->string('original_position_name', 100)->nullable();
            $table->decimal('original_salary', 10, 2)->nullable(); // 原薪资
            $table->string('original_salary_grade', 50)->nullable(); // 原薪资等级

            // 试岗岗位信息
            $table->unsignedBigInteger('probation_department_id');
            $table->string('probation_department_name', 100);
            $table->unsignedBigInteger('probation_position_id');
            $table->string('probation_position_name', 100);
            $table->decimal('probation_salary', 10, 2)->nullable(); // 试岗薪资
            $table->string('probation_salary_grade', 50)->nullable(); // 试岗薪资等级

            // 试岗时间安排
            $table->datetime('probation_start_date'); // 试岗开始日期
            $table->datetime('probation_end_date'); // 试岗结束日期
            $table->integer('probation_duration_days')->nullable(); // 试岗天数
            $table->boolean('is_extended')->default(false); // 是否延期
            $table->datetime('extended_end_date')->nullable(); // 延期结束日期
            $table->text('extension_reason')->nullable(); // 延期原因

            // 试岗期间待遇
            $table->boolean('maintain_original_salary')->default(true); // 是否保持原待遇
            $table->decimal('salary_adjustment', 10, 2)->nullable(); // 薪资调整金额
            $table->boolean('include_probation_bonus')->default(false); // 是否包含试岗奖金
            $table->decimal('probation_bonus_amount', 10, 2)->nullable(); // 试岗奖金金额

            // 考核信息
            $table->integer('assessment_frequency')->default(1); // 考核频率（月）
            $table->json('assessment_criteria')->nullable(); // 考核标准
            $table->json('assessment_schedule')->nullable(); // 考核计划

            // 考核记录
            $table->json('assessment_records')->nullable(); // 考核记录数组
            $table->decimal('final_assessment_score', 5, 2)->nullable(); // 最终考核分数
            $table->string('assessment_result', 50)->nullable(); // excellent, good, satisfactory, poor
            $table->text('assessment_comments')->nullable(); // 考核评语

            // 状态管理
            $table->string('status', 20)->default('active'); // active, completed, extended, terminated
            $table->datetime('actual_end_date')->nullable(); // 实际结束日期
            $table->string('termination_reason', 100)->nullable(); // 终止原因

            // 结果处理
            $table->string('final_result', 50)->nullable(); // confirmed, returned, terminated
            $table->boolean('salary_adjustment_applied')->default(false); // 是否已应用薪资调整
            $table->decimal('retroactive_amount', 10, 2)->nullable(); // 追溯调整金额
            $table->datetime('retroactive_date')->nullable(); // 追溯调整日期

            // 审批信息
            $table->string('approved_by', 100)->nullable();
            $table->datetime('approved_at')->nullable();
            $table->text('approval_notes')->nullable();

            // 试岗导师
            $table->unsignedBigInteger('mentor_id')->nullable();
            $table->string('mentor_name', 100)->nullable();
            $table->string('mentor_contact', 100)->nullable();

            // 备注和附件
            $table->text('notes')->nullable();
            $table->json('attachments')->nullable(); // 试岗计划、考核表等

            // 创建者信息
            $table->string('created_by', 100)->nullable();
            $table->string('updated_by', 100)->nullable();

            // 索引
            $table->index(['employee_id', 'status'], 'pr_emp_status_idx');
            $table->index(['probation_type', 'status'], 'pr_type_status_idx');
            $table->index(['probation_start_date', 'probation_end_date'], 'pr_dates_idx');
            $table->index(['status'], 'pr_status_idx');
            $table->index(['final_result'], 'pr_result_idx');
            $table->index(['mentor_id'], 'pr_mentor_idx');
            $table->index(['probation_number'], 'pr_number_idx');

            // 外键约束
            $table->foreign('employee_id')->references('id')->on('employees')->onDelete('cascade');
            $table->foreign('original_department_id')->references('id')->on('departments')->onDelete('set null');
            $table->foreign('original_position_id')->references('id')->on('positions')->onDelete('set null');
            $table->foreign('probation_department_id')->references('id')->on('departments')->onDelete('cascade');
            $table->foreign('probation_position_id')->references('id')->on('positions')->onDelete('cascade');
            $table->foreign('mentor_id')->references('id')->on('users')->onDelete('set null');

            // 唯一约束：员工在同一时间只能有一个试岗记录
            $table->unique(['employee_id', 'probation_start_date'], 'unique_employee_probation');
        });
    }

    /**
     * Reverse the migrations.
     */
    public function down(): void
    {
        Schema::dropIfExists('probation_records');
    }
};
